/**
 * Copyright &copy; com.nmf
 */
package com.nmf.site.biz.applybill.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nmf.dao.gen.BizApplyBillMapper;
import com.nmf.dao.gen.BizApplyBookMapper;
import com.nmf.dao.gen.bean.BizApplyBill;
import com.nmf.dao.gen.bean.BizApplyBillExample;
import com.nmf.dao.gen.bean.BizApplyBook;
import com.nmf.dao.gen.bean.BizApplyBookExample;
import com.nmf.site.common.persistence.Page;
import com.nmf.site.common.service.SysNameService;
import com.nmf.site.common.utils.IdGenerater;
import com.nmf.site.modules.sys.service.RoleService;
import com.nmf.site.modules.sys.utils.UserUtils;

/**
 * 申请表Service
 * @author bm
 * @version 2019-02-16
 */
@Service
@Transactional(readOnly = true)
public class BizApplyBillService{

	@Autowired
	BizApplyBillMapper bizApplyBillMapper;
	@Autowired
	IdGenerater idGenerater;
	
	public BizApplyBill get(String no) {
		return bizApplyBillMapper.selectByPrimaryKey(no);
	}
	
	public List<BizApplyBill> findList(BizApplyBill bizApplyBill) {
		BizApplyBillExample bizApplyBillExample = new BizApplyBillExample();
		BizApplyBillExample.Criteria criteria = bizApplyBillExample.createCriteria();
		
		return bizApplyBillMapper.selectByExample(bizApplyBillExample);
	}
	@Autowired
	RoleService roleService;
	public Page<BizApplyBill> findPage(Page<BizApplyBill> page, BizApplyBill bizApplyBill) {
		BizApplyBillExample bizApplyBillExample = new BizApplyBillExample();
		BizApplyBillExample.Criteria criteria = bizApplyBillExample.createCriteria();
		
		if(bizApplyBill.getBillStatus()!=null) {
			criteria.andBillStatusEqualTo(bizApplyBill.getBillStatus());
		}
		bizApplyBillExample.setPage(page);
		if(SysNameService.yttEnname.equals(roleService.queryRoleEnname(UserUtils.getUser().getId()))) {
			criteria.andOptIdEqualTo(UserUtils.getUser().getId());
		}
		List<BizApplyBill> dlist = bizApplyBillMapper.selectByExample(bizApplyBillExample);
		page.setList(dlist);
		page.setCount(bizApplyBillMapper.countByExample(bizApplyBillExample));
		
		return page;
	}
	
	public Page<BizApplyBill> findPageByProvince(Page<BizApplyBill> page, BizApplyBill bizApplyBill) {
		BizApplyBillExample bizApplyBillExample = new BizApplyBillExample();
		BizApplyBillExample.Criteria criteria = bizApplyBillExample.createCriteria();
		
		if(bizApplyBill.getBillStatus()!=null) {
			criteria.andBillStatusEqualTo(bizApplyBill.getBillStatus());
		}
		if(StringUtils.isNotBlank(UserUtils.getUserAreaId())) {
			criteria.andProvinceIdEqualTo(UserUtils.getUserAreaId());
		}
		bizApplyBillExample.setPage(page);
		
		List<BizApplyBill> dlist = bizApplyBillMapper.selectByExample(bizApplyBillExample);
		page.setList(dlist);
		page.setCount(bizApplyBillMapper.countByExample(bizApplyBillExample));
		
		return page;
	}
	
	
	@Transactional(readOnly = false)
	public void save(BizApplyBill bizApplyBill) {
		if(StringUtils.isNotEmpty(bizApplyBill.getId())&&get(bizApplyBill.getId())!=null) {
			bizApplyBillMapper.updateByPrimaryKeySelective(bizApplyBill);
		}else {
			Date date = new Date();
			bizApplyBill.setCreateDate(date);
			bizApplyBill.setId(idGenerater.genClsNo("bizApplyBill"));
			bizApplyBill.setUpdateDate(date);
			bizApplyBill.setOptId(UserUtils.getUser().getId());
			bizApplyBill.setOptName(UserUtils.getUser().getName());
			bizApplyBillMapper.insertSelective(bizApplyBill);
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(BizApplyBill bizApplyBill) {
		bizApplyBillMapper.deleteByPrimaryKey(bizApplyBill.getId());
	}
	@Autowired
	BizApplyBookMapper bizApplyBookMapper;
	public BizApplyBook forForm(String bookId) {
		BizApplyBookExample bizApplyBookExample = new BizApplyBookExample();
		BizApplyBookExample.Criteria criteria = bizApplyBookExample.createCriteria();
		String userId = UserUtils.getUser().getId();
		criteria.andUserIdEqualTo(userId);
		criteria.andIdEqualTo(bookId);
		List<BizApplyBook> blist =  bizApplyBookMapper.selectByExample(bizApplyBookExample);
		return blist.get(0);
	}
	
}